package models

import (
	"time"

	"github.com/astaxie/beego/orm"
	"fmt"
	"read.com/admin/libs"
)

type LogLogin struct {
	Id        int       `orm:"column(id);auto"`
	UserId    string    `orm:"column(user_id);size(8)" description:"用户ID"`
	LoginApp  int       `orm:"column(login_app)" description:"应用ID"`
	LoginOs   int8      `orm:"column(login_os)" description:"OS"`
	LoginIp   string    `orm:"column(login_ip);size(15)" description:"登录IP"`
	LoginDate time.Time `orm:"column(login_date);type(datetime)" description:"登录日期"`
	LoginMode int8      `orm:"column(login_mode)" description:"登录模式"`
	UserAgent string    `orm:"column(user_agent);size(255)" description:"用户代理"`
	LoginYmd  time.Time `orm:"column(login_ymd);type(date)" description:"年月日"`
	LoginHour int8      `orm:"column(login_hour)" description:"小时"`
}

type HourAndNum struct {
	LoginHour int8
	LoginNum  int
}

type HourAndPay struct {
	PayCount int
	PayHour  int
}

func (t *LogLogin) TableName() string {
	return "log_login"
}

func init() {
	orm.RegisterModel(new(LogLogin))
}

func GetLoginCountPayCount(startTime, endTime string) (error, map[string]interface{}) {
	o := orm.NewOrm()
	hurAndNums := make([]*HourAndNum, 0)
	hourAndPay := make([]*HourAndPay, 0)
	if startTime == "" && endTime == "" {
		dateTime := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
		start := time.Now().AddDate(0, 0, -1).Format("2006-01-02 00:00:00")
		end := time.Now().Format("2006-01-02 00:00:00")
		sqlStr := "SELECT COUNT(DISTINCT(`user_id`)) as login_num,login_hour FROM log_login WHERE login_ymd = '" + dateTime + "' GROUP BY login_hour ORDER BY login_hour"
		_, err := o.Raw(sqlStr).QueryRows(&hurAndNums)
		sqlStr = "SELECT COUNT(DISTINCT(user_id)) as pay_count,hour(confirm_date) as pay_hour FROM pay_confirm WHERE confirm_date >= ? and confirm_date < ? GROUP BY hour(confirm_date)"
		_, err = o.Raw(sqlStr,start,end).QueryRows(&hourAndPay)
		if err != nil {
			fmt.Println("select count from log_login error", err)
			return err, nil
		}
	} else {
		sqlStr := "SELECT COUNT(DISTINCT(`user_id`)) as login_num,login_hour FROM log_login WHERE login_ymd between ? and ? GROUP BY login_hour ORDER BY login_hour"
		_, err := o.Raw(sqlStr, startTime, endTime).QueryRows(&hurAndNums)
		startTime = libs.StrToTimeDay(startTime).Format("2006-01-02 00:00:00")
		endTime = libs.StrToTimeDay(endTime).AddDate(0,0,1).Format("2006-01-02 00:00:00")
		sqlStr = "SELECT COUNT(DISTINCT(user_id)) as pay_count,hour(confirm_date) as pay_hour FROM pay_confirm WHERE confirm_date >= ? and confirm_date < ? GROUP BY hour(confirm_date)"
		_, err = o.Raw(sqlStr,startTime,endTime).QueryRows(&hourAndPay)
		if err != nil {
			fmt.Println("select count from log_login error", err)
			return err, nil
		}
	}
	loginNums := make([]int, 24)
	payNums := make([]int,24)
	for i := 0; i < 24; i++ {
		loginNums[i] = 0
		payNums[i] = 0
		for _, v := range hurAndNums {
			if int(v.LoginHour) == i {
				loginNums[i] = v.LoginNum
				break
			}
		}
		for _, v := range hourAndPay {
			if v.PayHour == i {
				payNums[i] = v.PayCount
				break
			}
		}
	}
	result := map[string]interface{}{
		"loginNums":loginNums,
		"payNums":payNums,
	}
	return nil, result
}
